<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../vue.js"></script>
  </head>

  <body>
    <div id="app">
      <p>搜索 <input type="text" v-model="goodInfo.search" /></p>
    </div>
    <script>
      Vue.config.productionTip = false;

      const vm = new Vue({
        el: "#app",
        data() {
          return {
            goodInfo: {
              search: "oppo",
              price: 1888,
              num: 10,
            },
          };
        },
        watch: {
          //1.只监听search的改变(监听对象的某一个属性的写法)
          /* "goodInfo.search"() {
            console.log("重新search");
          }, */

          //
          goodInfo: {
            //2.立即监听:初始数据加载后,没有改变的时候就会执行一次回调函数
            immediate: true,
            //3.深度监听
            deep: true,
            //监听的回调函数,当监听的内容发生改变就会触发
            //4.watch的回调函数接受两个参数,分别是新值和旧值,但是如果监听的是一个对象,新值和旧值是一样的(除非把对象直接替换了)
            handler(newVal, oldVal) {
              console.log("goodInfo中有内容发生改变了", newVal, oldVal);
            },
          },
        },
      });
    </script>
  </body>
</html>
